$(function() {
	var sortTypeId = getvl("sortTypeId");
	if(sortTypeId != '') {
		searchBySortType(sortTypeId);
	}
})

//商圈
TradeArea();
//分类
sortType();


var currentPage = 1;
var tradeAreaId = '';
var sortTypeId = '';



//列表
loadMyEssay(tradeAreaId, sortTypeId);

function loadMyEssay(tradeAreaId, sortTypeId) {
	$(document).ready(function() {
		init(currentPage);
	});

	function init(pageNo) {
		$("#goodsList").html("");
		$.ajax({
			url: urlcore + "/api/wantBuy/selectList?tradeAreaId=" + tradeAreaId + "&sortTypeId=" + sortTypeId +"&size=" + 32 + "&current=" + pageNo,
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				if(data.success == true) {
					$.each(data.data.list, function(i, n) {
						
						var coverUrl=n.coverUrl==null?'http://app.jiagangwangluo.com/api//ATTACHMENT/cab06117-6607-401a-843d-5ac103323fb6/default.png':n.coverUrl;
							coverUrl=isHasImg(coverUrl);//图片处理
						var thislist =
							'<dl>' +
							'<a target="_blank" href="4.1.0qiugou_detail.html?id=' + n.id + '">' +
							'<dt>' +
							'<img class="searchQiugouImg" src="' + coverUrl + '" />' +
							'</dt>' +
							'</a>' +
							'</dl>';
						$('#goodsList').append(thislist);
					});
					
					//固定宽高
					$(function(){
						var imgList1 = document.getElementsByClassName("searchQiugouImg");  
					    var w=290;
					    var h=290;
					    for(var i=0;i<imgList1.length;i++){
					        if(imgList1[i].width>w || imgList1[i].height>h){  
					            imgList1[i].width = w;  
					            imgList1[i].heigth = h;  
					        }  
					    }  
					})

					$("#pager").pager({
						pagenumber: pageNo,
						pagecount: data.data.pages,
						totalcount: data.data.total,
						buttonClickCallback: PageClick
					});

				} else if(data.code == 'OVERTIME') {
					var thisUrl = window.location.href;
					//alert(thisUrl);
					if(thisUrl.indexOf('1.0.0denglu.html') <= -1) {
						top.window.location.href = "1.0.0denglu.html";
					}

				} else {
					if(data.msg != '空数据') {
						layer.alert(data.msg)
					} else {
						$('#thiscount').text(0);
					}
				}

			},
			error: function() {
				layer.alert("error");
			}
		});
	}
	PageClick = function(pageclickednumber) {
		init(pageclickednumber);
	}
}


//按区域查询
function searchByTradeArea(tradeAreaId) {
	loadMyEssay(tradeAreaId, '');
}


//按分类查询
function searchBySortType(sortTypeId) {
	$.ajax({
		url: urlcore + "/api/sortType/selectOne?id=" + sortTypeId,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
			   $("#breadcrumbName").text(data.data.sortName);
			}
		},
		error: function() {
			layer.alert("error");
		}
	});

	loadMyEssay('', sortTypeId);
}

 
